package com.lantosec.hmrp.modules.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.util.MonitorUtils;
import com.lantosec.hmrp.modules.monitor.entity.ProcessEntity;
import com.lantosec.hmrp.modules.monitor.mapper.MonitorProcessMapper;
import com.lantosec.hmrp.modules.monitor.service.IMonitorProcessService;
import com.lantosec.hmrp.modules.sysdata.entity.SysDataProcess;
import com.lantosec.hmrp.modules.sysdata.service.ISysDataProcessService;
import com.lantosec.hmrp.modules.system.service.ISysUserService;
import com.lantosec.hmrp.modules.workflow.vo.SysDataProcessQueryVO;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service()
@Primary
@Log4j2
@SuppressWarnings("all")
public class MonitorProcessServiceImpl extends ServiceImpl<MonitorProcessMapper, ProcessEntity> implements IMonitorProcessService {

    @Autowired
    private ISysDataProcessService dataProcessService;

    @Autowired
    private ISysUserService userService;

    @Override
    public List<ProcessEntity> getProcessTypeTotal(String month, String year) {
        String startTime = null;
        String endTime = null;
        if(StringUtils.isNotBlank(month)){
            startTime = month + MonitorUtils.firstDayOfMonth();
            endTime = month + MonitorUtils.lastDayOfMonth();
        }else if(StringUtils.isNotBlank(year)){
            startTime = MonitorUtils.firstDayOfYear(year);
            endTime = MonitorUtils.lastDayOfYear(year);
        }else{
            startTime = MonitorUtils.getLastMonthFirstDay();
            endTime = MonitorUtils.getLastMonthLastDay();
        }
        List<ProcessEntity> param = baseMapper.getProcessTypeTotal(startTime,endTime);
        return param;
    }

    @Override
    public List<ProcessEntity> getProcessStatusTotal(String month, String year) {
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            startTime = month + MonitorUtils.firstDayOfMonth();
            endTime = month + MonitorUtils.lastDayOfMonth();
        } else if (StringUtils.isNotBlank(year)) {
            startTime = MonitorUtils.firstDayOfYear(year);
            endTime = MonitorUtils.lastDayOfYear(year);
        } else {
            startTime = MonitorUtils.getLastMonthFirstDay();
            endTime = MonitorUtils.getLastMonthLastDay();
        }
        List<ProcessEntity> param = baseMapper.getProcessStatusTotal(startTime, endTime);
        return param;
    }

    @Override
    public Page<SysDataProcess> queryProcessInstanceList(PageVO<SysDataProcess> page, String month,
                                 String year, String processTypeId, String processStatus) {
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            startTime = month + MonitorUtils.firstDayOfMonth();
            endTime = month + MonitorUtils.lastDayOfMonth();
        } else if (StringUtils.isNotBlank(year)) {
            startTime = MonitorUtils.firstDayOfYear(year);
            endTime = MonitorUtils.lastDayOfYear(year);
        } else {
            startTime = MonitorUtils.getLastMonthFirstDay();
            endTime = MonitorUtils.getLastMonthLastDay();
        }
        SysDataProcessQueryVO condition = new SysDataProcessQueryVO();
        condition.setProcessTypeId(processTypeId);
        condition.setProcessStatus(processStatus);
        condition.setStartTime(startTime);
        condition.setEndTime(endTime);
        Page<SysDataProcess> list = dataProcessService.queryProcessInstanceList(page, condition);
        return page.setRecords(list.getRecords());
    }

    @Override
    public ProcessEntity getProcessTotal(String month, String year) {
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            startTime = month + MonitorUtils.firstDayOfMonth();
            endTime = month + MonitorUtils.lastDayOfMonth();
        } else if (StringUtils.isNotBlank(year)) {
            startTime = MonitorUtils.firstDayOfYear(year);
            endTime = MonitorUtils.lastDayOfYear(year);
        } else {
            startTime = MonitorUtils.getLastMonthFirstDay();
            endTime = MonitorUtils.getLastMonthLastDay();
        }
        return baseMapper.getProcessTotal(startTime,endTime);
    }
}
